<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑物品 - 归物</title>

    <!-- 网站图标 -->
    <link rel="icon" type="image/png" href="/icon.png">
    <link rel="shortcut icon" type="image/png" href="/icon.png">
    <link rel="apple-touch-icon" href="/icon.png">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .navbar-brand {
            font-weight: bold;
            color: #007bff !important;
        }
        .form-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 0.75rem 1rem;
        }
        .form-control:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(0,123,255,0.25);
        }
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 10px;
            padding: 0.75rem 2rem;
            font-weight: bold;
        }
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        .bottom-nav {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: white;
            border-top: 1px solid #dee2e6;
            z-index: 1000;
        }
        .bottom-nav .nav-link {
            color: #6c757d;
            text-align: center;
            padding: 0.75rem 0;
        }
        .bottom-nav .nav-link.active {
            color: #007bff;
        }
        .bottom-nav .nav-link i {
            font-size: 1.2rem;
            display: block;
            margin-bottom: 0.25rem;
        }
        .main-content {
            padding-bottom: 80px;
        }
        .form-text {
            font-size: 0.875rem;
            color: #6c757d;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container">
            <a class="navbar-brand" href="/dashboard">
                <i class="fas fa-boxes"></i> 归物
            </a>
            <div class="navbar-nav ms-auto">
                <div class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user"></i> <span th:text="${#authentication.principal.username}">用户</span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="/profile"><i class="fas fa-user-cog"></i> 个人设置</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li>
                            <form th:action="@{/logout}" method="post" style="display: inline;">
                                <button type="submit" class="dropdown-item">
                                    <i class="fas fa-sign-out-alt"></i> 退出登录
                                </button>
                            </form>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="main-content">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8 col-lg-6">
                    <div class="form-card p-4 mt-4">
                        <div class="text-center mb-4">
                            <h3><i class="fas fa-edit"></i> 编辑物品</h3>
                            <p class="text-muted">修改物品信息</p>
                        </div>

                        <!-- 错误消息 -->
                        <div th:if="${error}" class="alert alert-danger" role="alert">
                            <i class="fas fa-exclamation-triangle"></i>
                            <span th:text="${error}">错误信息</span>
                        </div>

                        <form th:action="@{/item/edit/{id}(id=${itemDto.id})}" th:object="${itemDto}" method="post">
                            <input type="hidden" th:field="*{id}">
                            
                            <div class="mb-3">
                                <label for="name" class="form-label">
                                    <i class="fas fa-tag"></i> 物品名称 *
                                </label>
                                <input type="text" class="form-control" id="name" th:field="*{name}" 
                                       placeholder="请输入物品名称" required>
                                <div th:if="${#fields.hasErrors('name')}" class="text-danger">
                                    <span th:errors="*{name}">名称错误</span>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="categoryId" class="form-label">
                                    <i class="fas fa-folder"></i> 分类 *
                                </label>
                                <div class="input-group">
                                    <select class="form-control" id="categoryId" name="categoryId" required>
                                        <option value="">请选择分类</option>
                                    </select>
                                    <button class="btn btn-outline-secondary" type="button" id="addCategoryBtn">自定义</button>
                                </div>
                                <div id="addCategoryBox" style="display:none;margin-top:8px;">
                                    <input type="text" class="form-control mb-2" id="newCategoryName" placeholder="新分类名称">
                                    <button class="btn btn-sm btn-primary" type="button" id="saveCategoryBtn">添加</button>
                                    <button class="btn btn-sm btn-link" type="button" id="cancelCategoryBtn">取消</button>
                                </div>
                                <div th:if="${#fields.hasErrors('categoryId')}" class="text-danger">
                                    <span th:errors="*{categoryId}">分类错误</span>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="price" class="form-label">
                                    <i class="fas fa-yen-sign"></i> 价格 *
                                </label>
                                <div class="input-group">
                                    <span class="input-group-text">¥</span>
                                    <input type="number" class="form-control" id="price" th:field="*{price}" 
                                           placeholder="请输入价格" step="0.01" min="0" required>
                                </div>
                                <div th:if="${#fields.hasErrors('price')}" class="text-danger">
                                    <span th:errors="*{price}">价格错误</span>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="purchaseDate" class="form-label">
                                    <i class="fas fa-calendar"></i> 入手时间 *
                                </label>
                                <input type="date" class="form-control" id="purchaseDate" th:field="*{purchaseDate}" 
                                       th:max="${#temporals.format(#temporals.createToday(), 'yyyy-MM-dd')}" required>
                                <div class="form-text">选择您购买或获得这个物品的日期</div>
                                <div th:if="${#fields.hasErrors('purchaseDate')}" class="text-danger">
                                    <span th:errors="*{purchaseDate}">日期错误</span>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="description" class="form-label">
                                    <i class="fas fa-align-left"></i> 描述
                                </label>
                                <textarea class="form-control" id="description" th:field="*{description}" 
                                          rows="3" placeholder="请输入物品描述（可选）"></textarea>
                                <div th:if="${#fields.hasErrors('description')}" class="text-danger">
                                    <span th:errors="*{description}">描述错误</span>
                                </div>
                            </div>

                            <div class="d-grid gap-2">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save"></i> 保存修改
                                </button>
                                <a href="/dashboard" class="btn btn-outline-secondary">
                                    <i class="fas fa-arrow-left"></i> 返回
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 底部导航栏 -->
    <div class="bottom-nav">
        <div class="container">
            <div class="row text-center">
                <div class="col-4">
                    <a href="/dashboard" class="nav-link">
                        <i class="fas fa-chart-line"></i>
                        <small>资产</small>
                    </a>
                </div>
                <div class="col-4">
                    <a href="/item/add" class="nav-link">
                        <i class="fas fa-plus"></i>
                        <small>添加</small>
                    </a>
                </div>
                <div class="col-4">
                    <a href="/profile" class="nav-link">
                        <i class="fas fa-user"></i>
                        <small>我的</small>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script th:inline="javascript">
        /*<![CDATA[*/
        var selectedCategoryId = /*[[${itemDto.categoryId}]]*/ null;
        function loadCategories(selectedId) {
            fetch('/api/category/list').then(r => r.json()).then(data => {
                const select = document.getElementById('categoryId');
                select.innerHTML = '<option value="">请选择分类</option>';
                data.forEach(c => {
                    select.innerHTML += `<option value="${c.id}"${selectedId==c.id?' selected':''}>${c.name}</option>`;
                });
            });
        }
        loadCategories(selectedCategoryId);
        document.getElementById('addCategoryBtn').onclick = function() {
            document.getElementById('addCategoryBox').style.display = '';
        };
        document.getElementById('cancelCategoryBtn').onclick = function() {
            document.getElementById('addCategoryBox').style.display = 'none';
            document.getElementById('newCategoryName').value = '';
        };
        document.getElementById('saveCategoryBtn').onclick = function() {
            const name = document.getElementById('newCategoryName').value.trim();
            if (!name) return alert('请输入分类名称');
            fetch('/api/category/add', {method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'name='+encodeURIComponent(name)})
                .then(r => r.json()).then(ok => {
                    if(ok) {
                        loadCategories();
                        document.getElementById('addCategoryBox').style.display = 'none';
                        document.getElementById('newCategoryName').value = '';
                    } else {
                        alert('添加失败，可能已存在');
                    }
                });
        };
        /*]]>*/
    </script>
</body>
</html> 